/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.BugRS.buz;

import com.aptech.BugRS.dao.CustomerDAO;
import com.aptech.BugRS.enity.CustomerDTO;
import java.util.ArrayList;

/**
 *
 * @author KISS
 */
public class CustomerBUZ {

    CustomerDAO dao = null;
    private String lastError;

    public CustomerBUZ() {
        dao = new CustomerDAO();
    }

    /**
     *
     * @param Customer
     * @return false if error else return true;
     */
    public boolean create(CustomerDTO cus) {
        if (!cus.Validate()) {
            //this.lastError = history.getValidateDescription();
            setLastError("E_Customer error");
            return false;
        }
        if (dao.isExits(cus)) {
            this.lastError = dao.getLastError();
            return false;
        }
        if (!dao.Create(cus)) {
            this.lastError = dao.getLastError();
            return false;
        }
        this.setLastError(dao.getLastError());
        return true;
    }

    /**
     * Read all Customer
     * @return list of Customer
     */
    public ArrayList<CustomerDTO> findByAll() {
        return dao.ReadByAll();
    }

    /**
     * Read by ID
     * @param id
     * @return Customer
     */
    public CustomerDTO findByID(int id) {
        return dao.ReadByID(id);
    }

    /**
     * Read by Email of customer
     * @param email
     * @return Customer
     */
    public CustomerDTO findByEmail(String email) {
        return dao.ReadByEmail(email);
    }

    /**
     * update Customer
     * @param customer
     * @return boolean
     */
    public boolean store(CustomerDTO cus) {
        if (!cus.Validate()) {
            setLastError("Entity Complaint Error");
            return false;
        }
        if (!dao.Update(cus)) {
            this.lastError = dao.getLastError();
            return false;
        }
        setLastError(dao.getLastError());
        return true;
    }

    /**
     * Read by LastName of Customer
     * @param lastName
     * @return list of Customer
     */
    public ArrayList<CustomerDTO> findByName(String lastName) {
        return dao.ReadByName(lastName);
    }

    /**
     * @return the lastError
     */
    public String getLastError() {
        return lastError;
    }

    /**
     * @param lastError the lastError to set
     */
    public void setLastError(String lastError) {
        this.lastError = lastError;
    }
}
